﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.Search.TxMainSearchControl.TxSearchItemITxObject
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using EngineeringInternalExtension;
using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Reflection;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using Tecnomatix.Engineering;
using UiInternalControls.WPFControls.SearchControl;


namespace DnProcessSimulateCommands.Search.TxMainSearchControl;

internal class TxSearchItemITxObject : TxSearchItemBase, INotifyPropertyChanged
{
  private ITxObject _object;

  public event PropertyChangedEventHandler PropertyChanged;

  protected virtual void OnPropertyChanges(string propertyName)
  {
    if (this.PropertyChanged == null)
      return;
    this.PropertyChanged((object) this, new PropertyChangedEventArgs(propertyName));
  }

  public ITxObject Object
  {
    get => this._object;
    set
    {
      if (this._object != null && this._object != value)
        this._object.NameChanged -= new TxObject_NameChangedEventHandler(this._object_NameChanged);
      this._object = value;
      if (this._object != null)
        this._object.NameChanged += new TxObject_NameChangedEventHandler(this._object_NameChanged);
      this.OnPropertyChanges("Caption");
    }
  }

  public virtual ImageSource Icon
  {
    set
    {
    }
    get
    {
      ImageSource icon = (ImageSource) null;
      if (this.Object != null)
        icon = TxIconManager.the().GetObjectImageSource(this.Object);
      if (icon == null)
        icon = (ImageSource) new BitmapImage(new Uri($"pack://application:,,,/{Assembly.GetExecutingAssembly().GetName().Name};component/Search/TxMainSearchControl/Resources/EmptyResultIcon.png", UriKind.Absolute));
      return icon;
    }
  }

  public virtual string Caption
  {
    set
    {
    }
    get => this.Object == null ? "" : this.Object.Name;
  }

  public virtual bool IsEnabled
  {
    set
    {
    }
    get => true;
  }

  public virtual string Tooltip
  {
    set
    {
    }
    get => (string) null;
  }

  public TxSearchItemITxObject(ITxObject obj) => this.Object = obj;

  protected virtual void Dispose(bool disposing)
  {
    if (!disposing || this._object == null)
      return;
    this._object.NameChanged -= new TxObject_NameChangedEventHandler(this._object_NameChanged);
  }

  public virtual void SelectionChanged()
  {
    TxObjectList txObjectList = new TxObjectList();
    ((Collection<ITxObject>) txObjectList).Add(this.Object);
    TxApplication.ActiveSelection.Clear();
    TxApplication.ActiveSelection.AddItems(txObjectList);
    base.SelectionChanged();
  }

  private void _object_NameChanged(object sender, TxObject_NameChangedEventArgs args)
  {
    this.OnPropertyChanges("Caption");
  }
}
